
// 生成较暗色
// color： 要处理的颜色值，生成rgb值
// percentage： 变暗的百分比 0-1
export function makeColorLight(color, percentage) {
	var rgb = {
		r: Math.max(0, color.r - (color.r * percentage)),
		g: Math.max(0, color.g - (color.g * percentage)),
		b: Math.max(0, color.b - (color.b * percentage))
	};
	rgb.r = parseInt(rgb.r);
	rgb.g = parseInt(rgb.g);
	rgb.b = parseInt(rgb.b);
	return `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`;
}

// 生成较浅色
// color： 要处理的颜色值，生成rgb值
// percentage： 变暗的百分比 0-1
export function makeLightColor (hexColor, percentage = 0.5) {
	if (!hexColor) return;
	let rgb = colorToRgb(hexColor);
	let min = Math.min(...rgb);
	if (percentage <= 0) {
		percentage = 0.1;
	} else if (percentage >= 1) {
		percentage = 0.9;
	}
	percentage *= 10;
	let full = 255 - min;
	let step = parseInt(full / 10);
	let lightPer = parseInt(step * percentage);
	rgb[0] = rgb[0] + lightPer > 255? 255: rgb[0] + lightPer;
	rgb[1] = rgb[1] + lightPer > 255? 255: rgb[1] + lightPer;
	rgb[2] = rgb[2] + lightPer > 255? 255: rgb[2] + lightPer;
	let hex = RgbToColor(rgb);
	return hex;
}

// 生成反差色
// 用于背景下的字体颜色
export function makeContrast (hexColor) {
	if (!hexColor) return;
	let rgb = colorToRgb(hexColor);
	let sum = rgb[0] + rgb[1] + rgb[2];
	let average = parseInt(sum / 3);
	let result = 255 - average;
	if (255 / 2 - average < 0 && Math.abs(255 / 2 - average) < 20) {
		result = result - 80 < 0? 0 : result - 80;
	}
	if (255 / 2 - average >= 0 && Math.abs(255 / 2 - average) < 20) {
		result = result + 80 > 255? 255 : result + 80;
	}
	rgb[0] = parseInt(result);
	rgb[1] = parseInt(result);
	rgb[2] = parseInt(result);
	let hex = RgbToColor(rgb);
	return hex;
}

// 生成黑白色
// 根据提供的颜色，生成其反差色的黑白色
export function makeContrastWB (hexColor) {
	if (!hexColor) return;
	let rgb = colorToRgb(hexColor);
	let sum = rgb[0] + rgb[1] + rgb[2];
	let average = parseInt(sum / 3);
	let result = 0;
	if (average > 255 / 2) {
		result = 0;
	} else {
		result = 255;
	}
	rgb[0] = parseInt(result);
	rgb[1] = parseInt(result);
	rgb[2] = parseInt(result);
	let hex = RgbToColor(rgb);
	return hex;
}

// 十六进制颜色值转rgb
export function colorToRgb(hexColor) {
	if (!hexColor) return;
	let reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
	let sColor = hexColor.toLowerCase()
	if (sColor && reg.test(sColor)) {
		if (sColor.length === 4) {
			let sColorNew = "#"
			for (let i = 1; i < 4; i += 1) {
				sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
			}
			sColor = sColorNew
		}
		//处理六位的颜色值
		let sColorChange = []
		for (let i = 1; i < 7; i += 2) {
			sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)))
		}
		return sColorChange
	} else {
		return sColor
	}
}

// rgb转十六进制颜色值
// rgb: 数组
export function RgbToColor(rgb) {
	let r = rgb[0].toString(16).length < 2? `0${rgb[0].toString(16)}`:rgb[0].toString(16);
	let g = rgb[1].toString(16).length < 2? `0${rgb[1].toString(16)}`:rgb[1].toString(16);
	let b = rgb[2].toString(16).length < 2? `0${rgb[2].toString(16)}`:rgb[2].toString(16);
	let hex = `#${r}${g}${b}`;
	return hex;
}






// let color = "#49F457";
// var originalColor = {r: res[0], g: res[1], b: res[2]};
// var lightColor = makeColorLight(originalColor, 0.2); // 参数是亮度减少的百分比
// let finalColor = 'linear-gradient(to right, ' + color +', '+ lightColor +')';
